Online-Academy
Look, Read, Understand, Apply

Operating System

File Management

  1. What is a File from an Operating System's perspective?

    Answer: A file is a logical unit of storage created by the OS to store data, programs, or information on secondary storage (like a hard drive or SSD). It is an abstraction that provides a way to store and retrieve data persistently, independent of the physical details of the storage device. To the OS, a file is simply a named collection of related information (bytes) stored on a disk.

  2. What are the typical attributes of a file?

    Answer: The OS maintains metadata about each file, often stored in a data structure called the file's i-node (in Unix) or Master File Table (MFT) entry (in Windows NTFS). Common attributes include:

    1. Name: The human-readable name (e.g., report.pdf).
    2. Identifier: A unique tag (often a number) that identifies the file within the file system.
    3. Type: The file format (e.g., .txt, .exe, .jpg) which tells the OS and applications how to interpret the data.
    4. Location: Pointer to where the file is stored on the disk (e.g., block numbers).
    5. Size: Current file size (in bytes) and possibly a maximum allowed size.
    6. Protection: Access control information (e.g., read/write/execute permissions for owner, group, others).
    7. Timestamps: Creation date, last modification date, and last access date.

  3. What are the basic operations that can be performed on a file?

    Answer: The OS provides a standard set of system calls for file operations:

    • Create: Allocate space and create a directory entry for the new file.
    • Open: Load the file's metadata into memory so it can be accessed quickly, returning a file descriptor.
    • Read: Copy data from the file into a user buffer.
    • Write: Copy data from a user buffer into the file.
    • Seek: Move the file pointer (the current read/write location) to a specific position within the file.
    • Close: Flush any buffered data to disk and release the file descriptor.
    • Delete/Unlink: Remove the file's directory entry and release its allocated disk space.
    • Truncate: Erase the file's contents but keep its attributes (set size to 0).

  4. Explain the difference between Sequential Access and Direct (Random) Access.

    Answer:

    • Sequential Access: Data is read/written in a linear order, one record after another. To access a specific record, you must read all preceding records. The file pointer moves automatically forward. This is the only method for tape drives and is used for applications that process data in a batch (e.g., log files, audio/video streaming).
    • Direct (Random) Access: Data can be read or written at any specific location in the file without reading other records. The user provides a block number or offset to the seek() operation. This is essential for databases, indexing systems, and any application that needs to quickly jump to specific data. Modern file systems support direct access on block devices (HDDs/SSDs).

  5. What is the purpose of a File Pointer?

    Answer: A file pointer (or file offset) is a per-process variable that indicates the current location (position) within an open file for the next read or write operation.

    • When a file is opened, the pointer usually points to the beginning (position 0).
    • Each read() or write() operation advances the pointer by the number of bytes read/written.
    • A seek() system call explicitly changes the file pointer to a specified location, enabling direct access.

  6. What is a Directory? What information does it contain?

    Answer: A directory is a special type of file that serves as a container for organizing other files and subdirectories. It provides a logical structure (a hierarchical namespace) to the file system. A directory entry typically contains:

    • The file name.
    • A pointer or reference to the file's metadata structure (e.g., the i-node number in Unix).
    • (Optionally) File type and basic attributes.

  7. Explain the difference between Single-Level, Two-Level, and Tree-Structured Directories.

    Answer:
    - Single-Level Directory: A single directory containing all files for all users.
    • Pros: Very simple.
    • Cons: Naming conflicts (no two files can have the same name), difficult to organize, and poor scalability. (Used in early simple systems).
    - Two-Level Directory: A master directory for each user, and each user has their own subdirectory.
    • Pros: Solves naming conflicts (each user has their own namespace).
    • Cons: Users cannot easily group their own files into logical subcategories, and sharing between users is complex.
    - Tree-Structured Directory (Hierarchical): Users can create multiple subdirectories within their own directory, forming a tree of arbitrary depth.
    • Pros: Extremely flexible, supports logical grouping, and is the standard for modern OSes (Windows, Linux, macOS).
    • Cons: Requires more complex path management (absolute vs. relative paths).

  8. What is the difference between an Absolute Path and a Relative Path?

    Answer:

    • Absolute Path: The full path from the root directory of the file system (e.g., in Linux: /home/user/Documents/report.txt; in Windows: C:\Users\User\Documents\report.txt). It uniquely identifies a file regardless of the current working directory.
    • Relative Path: The path relative to the current working directory (e.g., Documents/report.txt if you are currently in /home/user). It is shorter and more convenient but depends on the user's current location in the file tree.

  9. What is a "Mount Point" in a file system?

    Answer: A mount point is a directory in an existing file system where an additional file system is attached (mounted). This allows multiple physical storage devices (or partitions) to appear as a single, seamless logical tree.
    - Example: On Linux, the root file system is mounted at /. A USB drive might be mounted at /mnt/usb. When you access /mnt/usb, you are actually accessing the root directory of the USB drive's file system. Mounting makes the contents of the external device accessible through the main directory tree.

  10. Explain Contiguous Allocation. What are its advantages and disadvantages?

    Answer: Each file occupies a set of contiguous blocks on the disk. The directory entry stores the starting block address and the length (total number of blocks) of the file.
    - Advantages:
    - Excellent read performance (sequential or random access) because the disk head has minimal movement.
    - Simple to implement.
    - Disadvantages:
    - Suffers from External Fragmentation over time (holes appear as files are deleted).
    - Difficult to grow a file dynamically; you need to know the maximum size upfront or move the file to a larger contiguous space.

  11. Explain Linked Allocation. How does it solve the fragmentation problem?

    Answer: Each file is stored as a linked list of disk blocks, which can be scattered anywhere on the disk. Each block contains a pointer to the next block in the file. The directory entry points to the first and last blocks.

    • Fragmentation: Completely eliminates external fragmentation because any free block can be used.
    • Disadvantages:
      • Poor random access: To access block 'n', you must sequentially traverse the first 'n-1' blocks.
      • Reliability: If a pointer in one block is corrupted or lost, the rest of the file is lost.
      • Overhead: Each block consumes a small amount of space for the pointer.
      • Variation: File Allocation Table (FAT) uses a separate table to hold all pointers, avoiding pointer storage in data blocks.

  12. Explain Indexed Allocation.

    Answer: Indexed allocation solves the problems of both contiguous and linked allocation. It brings all the pointers for a file together into a single structure called an index block. The index block is an array of disk block addresses. The directory entry points to this index block. To read block 'n', the OS reads the 'n-th' entry from the index block and directly accesses that disk block.

    • Advantages: Supports direct access efficiently. No external fragmentation.
    • Disadvantages: Wastes space on the index block itself (a fixed overhead). If files are very large, one index block may not be sufficient to store all pointers, leading to multi-level indexing (like Unix inodes with direct, single, double, and triple indirect pointers).

  13. Given a file system with a block size of 4KB and disk addresses (pointers) of 4 bytes, how large a file can be supported if the index block holds 256 direct pointers?

    Answer:

    • Number of pointers in one index block = Block Size / Pointer Size = 4096 bytes / 4 bytes = 1024 pointers.
    • If the index block holds 1024 direct pointers, the maximum file size = 1024 × 4KB = 4,096 KB = 4 MB.
    • (If using a multi-level index, the size becomes massively larger. For example, with single indirect, you get 1024 1024 4KB = 4GB, etc.)

  14. How does the OS keep track of free disk blocks?

    Answer: The OS maintains a free-space list to track which blocks on the disk are available for allocation. Common methods include:

    1. Bit Vector (Bitmap): A bit map where each bit represents a disk block. 1 = free, 0 = allocated.
      • Pros: Simple and efficient to find free contiguous blocks.
      • Cons: Requires a large contiguous chunk of memory.
    2. Linked List (Free List): All free blocks are linked together with pointers.
      • Pros: Simple, uses no large memory.
      • Cons: Traversing the list to find free space is slow.
    3. Grouping: Stores the addresses of multiple free blocks in the first free block. This allows for faster access to a large set of free blocks.
    4. Counting: Tracks the starting address and length of contiguous free blocks. Useful because multiple contiguous free blocks are common.

  15. What is the difference between a "bitmap" and a "free list" for tracking free blocks? Which is faster?

    Answer:

    • Bitmap: A fixed array of bits. Finding a free block means scanning the bitmap for a 1. It is faster for finding contiguous runs of free blocks (because you can check many blocks at once using bitwise operations).
    • Free List: A linked list of free blocks. Finding a free block means traversing the list, which is slower (I/O dependent). However, it uses less memory space.
    • Generally: The bitmap is preferred in modern systems for performance, even though it consumes a bit of memory.

  16. What is a "File Descriptor" (in Unix/Linux)?

    Answer: A file descriptor is a small, non-negative integer that acts as an abstract handle (index) for accessing a file or other I/O resource (like a pipe or network socket) in a Unix/Linux system.

    • When a process opens a file, the OS creates an entry in the process's file descriptor table and returns the integer index to the user process.
    • System calls like read(), write(), and close() use this file descriptor to identify the specific open file instance. Standard descriptors are: 0 (stdin), 1 (stdout), and 2 (stderr).

  17. What is the difference between a "Hard Link" and a "Soft Link" (Symbolic Link)?

    Answer:

    • Hard Link: A direct reference to the same physical file data on disk (it points to the same i-node).
      • You cannot have hard links across different file systems.
      • Deleting the original file does not delete the data until all hard links are removed.
      • It is essentially a duplicate directory entry.
    • Soft Link (Symbolic Link): A special file that contains the pathname (absolute or relative) of another file.
      • It can point to files across different file systems.
      • If the target file is deleted, the soft link becomes a "dangling" or broken link.
      • It is effectively a shortcut.

  18. What is a "Journaling" file system? Give an example and explain why it is important.

    Answer: A journaling file system (e.g., ext3/ext4 in Linux, NTFS in Windows, APFS in macOS) maintains a special log file called a journal.

    • How it works: Before making any actual changes to the file system metadata (like allocation bitmaps or directory entries), the OS writes a brief record of what it intends to do to the journal. Once that "transaction" is safely written to disk, it executes the actual operation.
    • Why important: If the system crashes during a metadata update, the file system can check the journal on reboot. It can replay (redo) the committed transactions to bring the file system back to a consistent state, or roll back (undo) incomplete transactions. This prevents file system corruption and significantly speeds up recovery time (no need to run a slow fsck on a huge disk).

  19. A user creates a 10MB file on an empty disk using Contiguous Allocation. Later, they delete a 2MB file that was in the middle of the disk. Then, they try to create a new 4MB file. Will the OS be able to allocate space for the new file contiguously? Explain.

    Answer: It depends on the location of the 2MB hole.

    • Scenario A (No): If the 10MB file was placed immediately next to the 2MB hole, creating a single 12MB contiguous free space. Then the 4MB file can fit easily.
    • Scenario B (Yes, external fragmentation): If the 10MB file was placed before the 2MB hole, and another file was placed after the 2MB hole, the free space on the disk consists of fragmented holes. The OS will look for a single contiguous 4MB hole. If the largest hole is less than 4MB, the allocation will fail, even though the total free space is much larger than 4MB. This is the classic External Fragmentation problem.

  20. Explain how a "File System" is structured on a disk partition (Superblock, i-nodes, Data blocks).

    Answer: A disk partition is typically divided into the following logical sections:

    1. Boot Control Block (Boot Sector): Contains the boot loader program that the system executes to boot the OS. Usually resides at the very beginning of the partition.
    2. Volume Control Block (Superblock in Unix): Contains volume-specific metadata, such as the total number of blocks in the partition, the size of each block, the number of free blocks, the location of the free-space list or bitmap, and a pointer to the i-node table.
    3. Directory Structure / File Control Blocks (i-nodes): This section contains the metadata for every file and directory. In Unix, this is the i-node table. Each i-node stores attributes (size, permissions, timestamps) and pointers to the actual data blocks. In Windows NTFS, this is the Master File Table (MFT).
    4. Data Blocks: The largest section of the partition. This is where the actual contents (user data) of the files are stored.